package frist50;

import org.junit.jupiter.api.Test;

public class T0043 {
    public String multiply(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        int r1 = len1 - 1, r2 = len2 - 1;
        int carry = 0;
        StringBuffer re = new StringBuffer();
        while (r1 >= 0 && r2 >= 0) {
            int a = Integer.parseInt(String.valueOf(num1.charAt(r1)));
            int b = Integer.parseInt(String.valueOf(num2.charAt(r2)));
            int sum = a * b + carry;
            carry = sum / 10;
            re.insert(0, sum % 10);
            r1--;
            r2--;
        }
        while (r1 >= 0) {
            if (carry > 0) {
                int a = Integer.parseInt(String.valueOf(num1.charAt(r1)));
                int sum = a + carry;
                carry = sum / 10;
                re.insert(0, sum % 10);
                r1--;
                continue;
            }
            re.insert(0, num1.charAt(r1));
            r1--;
        }
        while (r2 >= 0) {
            if (carry > 0) {
                int a = Integer.parseInt(String.valueOf(num2.charAt(r2)));
                int sum = a + carry;
                carry = sum / 10;
                re.insert(0, sum % 10);
                r2--;
                continue;
            }
            re.insert(0, num2.charAt(r2));
            r2--;
        }
        return re.toString();
    }

    @Test
    public void test() {
        System.out.println(multiply("123", "456"));
    }
}
